function PublicFunction(obj) {
	this.init();
};

/**
 * 
 * 时间
 * 
 */

Date.prototype.format = function(format) { //给时间添加一个处理时间格式的函数new Date().format('yyyy-MM-dd hh');
	var date = {
		"M+": this.getMonth() + 1,
		"d+": this.getDate(),
		"h+": this.getHours(),
		"m+": this.getMinutes(),
		"s+": this.getSeconds(),
		"q+": Math.floor((this.getMonth() + 3) / 3),
		"S+": this.getMilliseconds()
	};
	if (/(y+)/i.test(format)) {
		format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length))
	}
	for (var k in date) {
		if (new RegExp("(" + k + ")").test(format)) {
			format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? date[k] : ("00" + date[k]).substr(("" + date[k]).length))
		}
	}
	return format
};

PublicFunction.prototype.init = function(obj) { //初始化变量
	var ua = navigator.userAgent.toLowerCase(); //微信判断
	this.WeChatFlag = ua.match(/MicroMessenger/i) == "micromessenger"; //微信环境判断
};

PublicFunction.prototype.getNowFormatDate = function() {
	var date = new Date();
	var seperator1 = "-";
	var year = date.getFullYear();
	var month = date.getMonth() + 1;
	var strDate = date.getDate();
	if (month >= 1 && month <= 9) {
		month = "0" + month;
	}
	if (strDate >= 0 && strDate <= 9) {
		strDate = "0" + strDate;
	}
	var currentdate = year + seperator1 + month + seperator1 + strDate;
	return currentdate;
}
PublicFunction.prototype.getNowFormatDateAfter = function(AddDayCount, date) { //获取AddDayCount天后的日期this.getNowFormatDateAfter(1)一天后
	var oDate = date ? this.getTimeFormat(date) : new Date();
	oDate.setDate(oDate.getDate() + AddDayCount);
	return oDate;
};

PublicFunction.prototype.getTimeFormat = function(date) { //兼容ios的时间格式处理
	var oDate = date.replace(/-/g, '/').replace(/\./g, '/');
	oDate = new Date(oDate);
	return oDate;
};

PublicFunction.prototype.getTime = function(date) { //处理时间
	var oDate = date ? this.getTimeFormat(date) : new Date();
	var y = oDate.getFullYear();
	var m = oDate.getMonth() + 1;
	var d = oDate.getDate();
	var h = oDate.getHours();
	var min = oDate.getMinutes();
	var s = oDate.getSeconds();
	return {
		y: y,
		m: m,
		d: d,
		h: h,
		min: min,
		s: s
	}
};

PublicFunction.prototype.getWeek = function(departureDate) { //获取周几并判断是否为今天
	var day = departureDate ? this.getTimeFormat(departureDate) : new Date(); //将日期值格式化
	departureDate = day.format('yyyy-MM-dd');
	var today = new Date().format('yyyy-MM-dd');
	var weekday = new Array("周日", "周一", "周二", "周三", "周四", "周五", "周六"); //创建星期数组
	var tomorrow = this.getNowFormatDateAfter(1).format('yyyy-MM-dd'); //获取明天的日期
	var dopodomani = this.getNowFormatDateAfter(2).format('yyyy-MM-dd'); //获取后天的日期
	var yesterday = this.getNowFormatDateAfter(-1).format('yyyy-MM-dd'); //获取昨天的日期
	var beforeYesterday = this.getNowFormatDateAfter(-2).format('yyyy-MM-dd'); //获取前天的日期
	var obj = {};
	obj.isToday = (departureDate == today); //是否为今天
	obj.isTomorrow = (departureDate == tomorrow); //是否为明天
	obj.isDopodomani = (departureDate == dopodomani); //是否为后天
	obj.isYesterday = (departureDate == yesterday); //是否为昨天
	obj.isBeforeYesterday = (departureDate == beforeYesterday); //是否为前天
	obj.week = weekday[day.getDay()]; //周几
	obj.day = day.format('MM月dd日'); //月日
	obj.time = day.format('hh:mm'); //时分
	obj.dateMsg = day;
	return obj;
};
PublicFunction.prototype.getWeekTwo = function(departureDate) { //获取周几并判断是否为今天
	var day = departureDate ? this.getTimeFormat(departureDate) : new Date(); //将日期值格式化
	departureDate = day.format('yyyy-MM-dd');
	var today = new Date().format('yyyy-MM-dd');
	var weekday = new Array("周日", "周一", "周二", "周三", "周四", "周五", "周六"); //创建星期数组
	var tomorrow = this.getNowFormatDateAfter(1).format('yyyy-MM-dd'); //获取明天的日期
	var dopodomani = this.getNowFormatDateAfter(2).format('yyyy-MM-dd'); //获取后天的日期
	var yesterday = this.getNowFormatDateAfter(-1).format('yyyy-MM-dd'); //获取昨天的日期
	var beforeYesterday = this.getNowFormatDateAfter(-2).format('yyyy-MM-dd'); //获取前天的日期
	var obj = {};
	obj.isToday = (departureDate == today); //是否为今天
	obj.isTomorrow = (departureDate == tomorrow); //是否为明天
	obj.isDopodomani = (departureDate == dopodomani); //是否为后天
	obj.isYesterday = (departureDate == yesterday); //是否为昨天
	obj.isBeforeYesterday = (departureDate == beforeYesterday); //是否为前天
	obj.week = weekday[day.getDay()]; //周几
	obj.day = day.format('MM-dd'); //月日
	obj.time = day.format('hh:mm'); //时分
	obj.dateMsg = day;
	return obj;
};
PublicFunction.prototype.isDateBetween = function(dateString, startDateString, endDateString) { //判断日期是否在设定区间内
	//参数 判断时间 开始时间 结束时间
	var flag = false;
	var startFlag = (this.dateCompare(dateString, startDateString) < 1);
	var endFlag = (this.dateCompare(dateString, endDateString) > -1);
	(startFlag && endFlag) && (flag = true);
	return flag
};

PublicFunction.prototype.dateCompare = function(dateString, compareDateString) { //判断日期的先后关系
	//结果 0为相同 1为后者大 -1为前者大
	var dateTime = this.getTimeFormat(dateString).getTime();
	var compareDateTime = this.getTimeFormat(compareDateString).getTime();
	var flag = 0;
	(compareDateTime > dateTime) && (flag = 1);
	(compareDateTime < dateTime) && (flag = -1);
	return flag;
};

PublicFunction.prototype.dateNum = function(y, m, d) { //获取该天是这一年的第几天 传入年，月，日 
	var dataOne = y + ".01.01";
	var dataTwo = y + "." + m + "." + d;
	var numOne = new Date(dataOne).getTime();
	var numTwo = new Date(dataTwo).getTime();
	day = (numTwo - numOne) / (1000 * 60 * 60 * 24);
	return day + 1;
};

PublicFunction.prototype.changeTime = function(runTime) { //改写时间
	return (Math.floor(runTime / 60) > 0) ? (Math.floor(runTime / 60) + '小时' + (runTime % 60) + '分钟') : (runTime + "分钟");
};

/**
 *
 * 正则
 * 
 */

PublicFunction.prototype.checkPhoneNum = function(val) { //手机号验证
	return /^1\d{10}$/.test(val);
};

PublicFunction.prototype.checkEmail = function(val) { //邮箱验证
	return /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/.test(val);
};

PublicFunction.prototype.checkName = function(val) { //姓名验证
	return /^([a-zA-Z0-9\u4e00-\u9fa5\·]{1,10})$/.test(val);
};

PublicFunction.prototype.checkIDnumber = function(val) { //身份证验证
	return /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|[xX])$/.test(val);
};

PublicFunction.prototype.checkHkAndMacao = function(val) { //港澳通行证验证
	return /^[HMhm]{1}([0-9]{10}|[0-9]{8})$/.test(val);
};

PublicFunction.prototype.checkPassport = function(val) { //护照验证
	return /^[a-zA-Z]{5,17}$/.test(val) || /^[a-zA-Z0-9]{5,17}$/.test(val);
};

PublicFunction.prototype.checkCompatriots = function(val) { //台胞证验证
	return /^[0-9]{8}$/.test(val) || /^[0-9]{10}$/.test(val);
};
/**
 *
 *  其他
 * 
 */

PublicFunction.prototype.onReachBottom = function(msg) { //下拉加载封装 传入执行函数 或者 非函数
	//	var oBody = document.getElementsByTagName("body")[0];//body
	function scroll() { // 判断是否滚动到底部
		//		var scrollTop = document.body.scrollTop || document.documentElement.scrollTop;//滚动条
		//		var offsetHeight = oBody.offsetHeight* 0.5;
		//		var flag = scrollTop >= offsetHeight *0.6;//滚动到距离底部百分之60%开始加载
		var scrollTop = document.documentElement.scrollTop || document.body.scrollTop; //距离顶部的距离
		var windowHeight = document.documentElement.clientHeight || document.body.clientHeight; //可视区的高度
		var scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight; //滚动条总高
		var flag = scrollTop + windowHeight >= scrollHeight * 0.9;
		(flag) && (msg && msg());
	};
	//不再需要监听时卸载
	(typeof msg == "function") ? window.addEventListener('scroll', scroll): window.removeEventListener('scroll', scroll);
};
/**
 * 倒计时定时器
 */
PublicFunction.prototype.getTimer = function(second, that) { //传服务器剩余时间（s）和this
	var lastTime = (new Date()).getTime(); //记录上次的时间初始值为开始
	var residueTime = second; //记录剩余的秒数
	var timer = setInterval(function() {
		var nowTime = (new Date()).getTime(); //获取现在的时间
		//计算时间差
		var time = (nowTime - lastTime) / 1000; //取整
		// console.log(second, time);
		//获得剩余的秒数
		residueTime = residueTime - time;
		//更新上一个时间
		lastTime = nowTime;
		//剩余的秒数转化格式
		if (residueTime > 0) {
			if (that.time) {
				that.time.min = Math.floor(residueTime / 60);
				that.time.s = (residueTime % 60).toFixed(0);
				//格式化时间
				that.time.min = Number(that.time.min);
				that.time.min = (that.time.min > 9) ? that.time.min : "0" + that.time.min;
				that.time.s = (that.time.s > 9) ? that.time.s : "0" + that.time.s;
			};
		} else { //归0时
			clearInterval(timer); //清除定时器
			if (that.time) {
				that.time.min = '00';
				that.time.s = '00';
			};
			//支付按钮变灰
			that.payFlag = false;
		}
	}, 1000);
	//返回定时器
	return timer;
};

PublicFunction.prototype.getDateDiff = function(val) {
	var date = new Date();
	date.setDate(1); //当月第一天
	var startTime = new Date().getTime();
	var endTime = val;
	console.log(startTime)
	console.log(endTime)
	var dates = Math.abs((date - endTime)) / (1000 * 60 * 60 * 24);
	return dates;
}
//姓名验证
PublicFunction.prototype.checkchname = function(val){
	if (/[^\u4e00-\u9fa5]{2,12}/g.test(val) && /[^·]/g.test(val)) {
		return false
	}
	return true 
}
PublicFunction.prototype.getQueryString = function(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
	var r = window.location.search.substr(1).match(reg);
	if (r != null) return unescape(r[2]);
	return null;
}

PublicFunction.prototype.getPageAllString = function() { //获取页面所有参数
	var mgs = {}; //参数结果
	var url = window.location.href; //获取路径得到传参
	var a = url.indexOf("?"); //根据问号得到传参的位置
	if(a > -1) {
		var b = url.substr(a + 1); //拿到参数的字符串
		var mgsArr = b.split("&"); //改写为一个数组
		mgsArr.forEach(function(val) { //遍历
			var arr = val.split("=");
			mgs[arr[0]] = arr[1];
		});
	};
	return mgs;
};

PublicFunction.prototype.DateMinus = function(endDate, startDate) { //hotel-bear 两个日期相减
	var endDate = new Date(endDate.replace(/-/g, "/"));
	var startDate = new Date(startDate.replace(/-/g, "/"));
	var days = endDate.getTime() - startDate.getTime();
	var day = parseInt(days / (1000 * 60 * 60 * 24));
	return day;
};
// var a = function (res,type) {
// 	var obj = {
// 		debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
// 		appId: 'wx348ec18fec6bf757', // 必填，公众号的唯一标识
// 		timestamp: res.data.Result.data.createTime, // 必填，生成签名的时间戳
// 		nonceStr: res.data.Result.data.nonce_str, // 必填，生成签名的随机串
// 		signature: res.data.Result.data.sign, // 必填，签名
// 		jsApiList: ["chooseWXPay","share"] // 必填，需要使用的JS接口列表
// 	};
// 	if(type == 1) {
// 		obj.jsApiList.push("chooseWXPay");
// 	} else if (type == 2) {
// 		obj.jsApiList.push("share");
// 	}
// 	wx.config(obj);
// 	wx.ready(function(res) {
// 		console.log(res)
// 	});
// }

//手机号验证
PublicFunction.prototype.checkMobile = function(val) {
	var re = /^1\d{10}$/
	if (re.test(val)) {
		return true 
	} else {
		return false
	}
}



var publicMethod = new PublicFunction();
export default publicMethod; //导出
